package com.ac.api.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.context.Context;
import com.base.context.ContextHolder;
import com.ac.api.component.dao.CpLabelMapper;
import com.ac.api.component.entity.CpVideo;
import com.ac.api.component.dao.CpVideoMapper;
import com.ac.api.component.service.CpVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ac.api.component.vo.CpLabelVo;
import com.ac.api.component.vo.CpVideoVo;
import com.ac.utils.FileUtils;
import com.ac.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gjj
 * @since 2024-11-12
 */
@Service
public class CpVideoServiceImpl extends ServiceImpl<CpVideoMapper, CpVideo> implements CpVideoService {
    @Autowired
    private CpLabelMapper cpLabelMapper;

    @Override
    public CpVideoVo saveVideo(CpVideoVo cpVideoVo) {
        Context context = ContextHolder.currentContext();
        //查找对应模块名称和标签名称的标签
        CpLabelVo cpLabelVo=cpLabelMapper.findByName(cpVideoVo.getModuleName(),cpVideoVo.getLabelName());
        if(cpLabelVo==null){
            return null;
        }
        cpVideoVo.setUserid(context.currentUser().getId());
        cpVideoVo.setCreatetime(new Date());
        cpVideoVo.setCpLabelId(cpLabelVo.getLabelId());
        cpVideoVo.setState("t");
        cpVideoVo.setSize(FileUtils.getFileLength(cpVideoVo.getPath())+"");
        this.save(cpVideoVo);
        return cpVideoVo;
    }

    @Override
    public CpVideoVo updateVideo(CpVideoVo cpVideoVo) {
        Context context = ContextHolder.currentContext();
        //查找对应模块名称和标签名称的标签
        CpLabelVo cpLabelVo=cpLabelMapper.findByName(cpVideoVo.getModuleName(),cpVideoVo.getLabelName());
        if(cpLabelVo==null){
            return null;
        }
        cpVideoVo.setUserid(context.currentUser().getId());
        cpVideoVo.setCpLabelId(cpLabelVo.getLabelId());
        cpVideoVo.setState("t");
        cpVideoVo.setSize(FileUtils.getFileLength(cpVideoVo.getPath())+"");
        this.updateById(cpVideoVo);
        return cpVideoVo;
    }

    @Override
    public Boolean deleteVideoById(Integer id) {
        return this.lambdaUpdate().set(CpVideo::getState,"f").eq(CpVideo::getId, id).update();
    }

    @Override
    public List<CpVideo> list(CpVideo example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<CpVideo> page(CpVideo example, IPage page) {
        return this.page(page,buildWrapper(example));
    }

    @Override
    public Page<CpVideoVo> findByPage(CpVideoVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(this.baseMapper.findByExample(buildCpPhotoVoWrapper(example), page));
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<CpVideoVo> buildCpPhotoVoWrapper(CpVideoVo example) {
        QueryWrapper<CpVideoVo> wrapper     = new QueryWrapper<>();
        wrapper.like(StringUtil.isNotBlank(example.getName()),"name",example.getName())
                .like(StringUtil.isNotBlank(example.getLabelName()),"label_name",example.getLabelName())
                .like(StringUtil.isNotBlank(example.getModuleName()),"module_name",example.getModuleName())
                .eq(example.getId()!=null,"id",example.getId())
                .eq("state","t")
                .orderByDesc("createtime");
        return wrapper;
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<CpVideo> buildWrapper(CpVideo example) {
        QueryWrapper<CpVideo> wrapper     = new QueryWrapper<>();
        return wrapper;
    }

}
